using System;
using System.Data;
using System.Data.Common;
using System.Data.SqlTypes;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BaHuan.Common;
using BaHuan.Entities;
/*
 * For each DB unit, normally tables, we create a DAO class for it
 * this will enalbe us to do some basic tasks for a DB unit
 * such as Save, Delete, GetAll, GetById....
 * 
 * */
namespace BaHuan.DataLayer
{
	
	class PhieuXuatKhoDAO:BaseDAO
	{
	
		// save into DB, @id = -1: insert else, update
		public ResultObj SavePhieuXuatKho(PhieuXuatKho phieuxuatkho)
        		{
			ResultObj result = new ResultObj();
			result.Result = null;
			SqlConnection conObj = DataConnection.GetSqlConnection();
			SqlCommand cmd = CreateSqlCommand("spAddPhieuXuatKho", CommandType.StoredProcedure);
			cmd.CommandType = CommandType.StoredProcedure;
			cmd.CommandText = "spAddPhieuXuatKho";
			SqlParameter param = new SqlParameter();
			param = CreateParameter("@Id", DbType.Int32, ParameterDirection.Input, phieuxuatkho.Id);
			cmd.Parameters.Add(param);
            param = CreateParameter("@Prefix", DbType.String, ParameterDirection.Input, phieuxuatkho.Prefix);
            cmd.Parameters.Add(param);
            param = CreateParameter("@OrderNumber", DbType.String, ParameterDirection.Input, phieuxuatkho.OrderNumber);
            cmd.Parameters.Add(param);
            param = CreateParameter("@MaSo", DbType.String, ParameterDirection.Input, phieuxuatkho.MaSo);
            cmd.Parameters.Add(param);
            param = CreateParameter("@History", DbType.String, ParameterDirection.Input, phieuxuatkho.History);
            cmd.Parameters.Add(param);
			param = CreateParameter("@NgayThang", DbType.DateTime, ParameterDirection.Input, phieuxuatkho.NgayThang);
			cmd.Parameters.Add(param);
			param = CreateParameter("@NguoiLapPhieu", DbType.String, ParameterDirection.Input, phieuxuatkho.NguoiLapPhieu);
			cmd.Parameters.Add(param);
			param = CreateParameter("@BoPhan", DbType.String, ParameterDirection.Input, phieuxuatkho.BoPhan);
			cmd.Parameters.Add(param);
			param = CreateParameter("@NguoiNhan", DbType.String, ParameterDirection.Input, phieuxuatkho.NguoiNhan);
			cmd.Parameters.Add(param);
			param = CreateParameter("@IdChiNhanhCongTy", DbType.Int32, ParameterDirection.Input, phieuxuatkho.IdChiNhanhCongTy);
			cmd.Parameters.Add(param);
			param = CreateParameter("@LyDo", DbType.String, ParameterDirection.Input, phieuxuatkho.LyDo);
			cmd.Parameters.Add(param);
			param = CreateParameter("@ChungTuKemTheo", DbType.String, ParameterDirection.Input, phieuxuatkho.ChungTuKemTheo);
			cmd.Parameters.Add(param);
			param = CreateParameter("@IsDinhKhoan", DbType.Int32, ParameterDirection.Input, phieuxuatkho.IsDinhKhoan);
			cmd.Parameters.Add(param);
            param = CreateParameter("@IsDelete", DbType.Int32, ParameterDirection.Input, phieuxuatkho.IsDelete);
            cmd.Parameters.Add(param);
			param = CreateParameter("@ReturnVal", DbType.String, ParameterDirection.ReturnValue, result.ReturnCode);
			cmd.Parameters.Add(param);
			conObj.Open();
			cmd.ExecuteNonQuery();
			conObj.Close();
			result.ReturnCode = Convert.ToInt32(cmd.Parameters["@ReturnVal"].Value);
			
			return result;
		}
		
		// Get all PhieuXuatKho in DB with IsDelete = 0
		public ResultObj GetAllPhieuXuatKho( )
        		{
			ResultObj result = new ResultObj();
			result.Result = null;
			List<PhieuXuatKho> lst = null;
            SqlCommand cmd = CreateSqlCommand("spGetAllPhieuXuatKho", CommandType.StoredProcedure);
			cmd.Connection.Open();
			using (SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
			{
			lst = new List<PhieuXuatKho>();
			PhieuXuatKho item = null;
			// reader all data from DB one by one
			while (reader.Read())
			{
			item = new PhieuXuatKho();
			item.Id = Convert.ToInt32(reader["Id"]);
            item.Prefix = Convert.ToString(reader["Prefix"]);
            item.OrderNumber = Convert.ToString(reader["OrderNumber"]);
            item.MaSo = Convert.ToString(reader["MaSo"]);
            item.History = Convert.ToString(reader["History"]);
			item.NgayThang = Convert.ToDateTime(reader["NgayThang"]);
			item.NguoiLapPhieu = Convert.ToString(reader["NguoiLapPhieu"]);
			item.BoPhan = Convert.ToString(reader["BoPhan"]);
			item.NguoiNhan = Convert.ToString(reader["NguoiNhan"]);
			item.IdChiNhanhCongTy = Convert.ToInt32(reader["IdChiNhanhCongTy"]);
			item.LyDo = Convert.ToString(reader["LyDo"]);
			item.ChungTuKemTheo = Convert.ToString(reader["ChungTuKemTheo"]);
			item.IsDinhKhoan = Convert.ToInt32(reader["IsDinhKhoan"]);
            item.IsDelete = Convert.ToInt32(reader["IsDelete"]);
			lst.Add(item);
			}
			}
			result.Result = lst;
			result.ReturnCode = 1;
			
			return result;
		}
		
		// Get all PhieuXuatKho in DB with condition
		public ResultObj GetAllPhieuXuatKhoByID(PhieuXuatKho phieuxuatkho)
        		{
			ResultObj result = new ResultObj();
			result.Result = null;
			List<PhieuXuatKho> lst = null;
			SqlCommand cmd = CreateSqlCommand("spGetAllPhieuXuatKhoByID", CommandType.StoredProcedure);
			SqlParameter param = CreateParameter("@Id", DbType.Int32, ParameterDirection.Input, phieuxuatkho.Id);
			cmd.Parameters.Add(param);
			cmd.Connection.Open();
			using (SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
			{
			lst = new List<PhieuXuatKho>();
			PhieuXuatKho item = null;
			// reader all data from DB one by one
			while (reader.Read())
			{
			item = new PhieuXuatKho();
			item.Id = Convert.ToInt32(reader["Id"]);
            item.Prefix = Convert.ToString(reader["Prefix"]);
            item.OrderNumber = Convert.ToString(reader["OrderNumber"]);
            item.MaSo = Convert.ToString(reader["MaSo"]);
            item.History = Convert.ToString(reader["History"]);
			item.NgayThang = Convert.ToDateTime(reader["NgayThang"]);
			item.NguoiLapPhieu = Convert.ToString(reader["NguoiLapPhieu"]);
			item.BoPhan = Convert.ToString(reader["BoPhan"]);
			item.NguoiNhan = Convert.ToString(reader["NguoiNhan"]);
			item.IdChiNhanhCongTy = Convert.ToInt32(reader["IdChiNhanhCongTy"]);
			item.LyDo = Convert.ToString(reader["LyDo"]);
			item.ChungTuKemTheo = Convert.ToString(reader["ChungTuKemTheo"]);
			item.IsDinhKhoan = Convert.ToInt32(reader["IsDinhKhoan"]);
            item.IsDelete = Convert.ToInt32(reader["IsDelete"]);
			lst.Add(item);
			}
			}
			result.Result = lst;
			result.ReturnCode = 1;
			
			return result;
		}

        public ResultObj GetAllPhieuXuatKhoByMaSo(PhieuXuatKho phieuxuatkho)
        {
            ResultObj result = new ResultObj();
            result.Result = null;
            List<PhieuXuatKho> lst = null;
            SqlCommand cmd = CreateSqlCommand("spGetAllPhieuXuatKhoByMaSo", CommandType.StoredProcedure);
            SqlParameter param = CreateParameter("@MaSo", DbType.String, ParameterDirection.Input, phieuxuatkho.MaSo);
            cmd.Parameters.Add(param);
            cmd.Connection.Open();
            using (SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
            {
                lst = new List<PhieuXuatKho>();
                PhieuXuatKho item = null;
                // reader all data from DB one by one
                while (reader.Read())
                {
                    item = new PhieuXuatKho();
                    item.Id = Convert.ToInt32(reader["Id"]);
                    item.Prefix = Convert.ToString(reader["Prefix"]);
                    item.OrderNumber = Convert.ToString(reader["OrderNumber"]);
                    item.MaSo = Convert.ToString(reader["MaSo"]);
                    item.History = Convert.ToString(reader["History"]);
                    item.NgayThang = Convert.ToDateTime(reader["NgayThang"]);
                    item.NguoiLapPhieu = Convert.ToString(reader["NguoiLapPhieu"]);
                    item.BoPhan = Convert.ToString(reader["BoPhan"]);
                    item.NguoiNhan = Convert.ToString(reader["NguoiNhan"]);
                    item.IdChiNhanhCongTy = Convert.ToInt32(reader["IdChiNhanhCongTy"]);
                    item.LyDo = Convert.ToString(reader["LyDo"]);
                    item.ChungTuKemTheo = Convert.ToString(reader["ChungTuKemTheo"]);
                    item.IsDinhKhoan = Convert.ToInt32(reader["IsDinhKhoan"]);
                    item.IsDelete = Convert.ToInt32(reader["IsDelete"]);
                    lst.Add(item);
                }
            }
            result.Result = lst;
            result.ReturnCode = 1;

            return result;
        }

		// Delete a PhieuXuatKho out of DB with condition
		public ResultObj DeletePhieuXuatKho(PhieuXuatKho phieuxuatkho)
        		{
			ResultObj result = new ResultObj();
			result.Result = null;
			SqlConnection conObj = DataConnection.GetSqlConnection();
			SqlCommand cmd = CreateSqlCommand("spDeletePhieuXuatKho", CommandType.StoredProcedure);
			cmd.CommandType = CommandType.StoredProcedure;
			cmd.CommandText = "spDeletePhieuXuatKho";
			SqlParameter param = CreateParameter("@Id", DbType.Int32, ParameterDirection.Input, phieuxuatkho.Id);
			cmd.Parameters.Add(param);
			conObj.Open();
			result.ReturnCode = cmd.ExecuteNonQuery();
			conObj.Close();
			
			return result;
		}
		
		// Get info of CTPhieuXuatKho from DB with condition
		public ResultObj GetCTPhieuXuatKho(int IdCTPhieuXuatKho)
        		{
			ResultObj result = new ResultObj();
			result.Result = null;
			List<CTPhieuXuatKho> lst = null;
			SqlCommand cmd = CreateSqlCommand("spGetCTPhieuXuatKhoByID", CommandType.StoredProcedure);
			SqlParameter param = CreateParameter("@Id", DbType.Int32, ParameterDirection.Input, IdCTPhieuXuatKho);
			cmd.Parameters.Add(param);
			cmd.Connection.Open();
			using (SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
			{
			lst = new List<CTPhieuXuatKho>();
			CTPhieuXuatKho item = null;
			// reader all data from DB one by one
			while (reader.Read())
			{
			item = new CTPhieuXuatKho();
			item.Id = Convert.ToInt32(reader["Id"]);
			item.IdPhieuXuatKho = Convert.ToInt32(reader["IdPhieuXuatKho"]);
			item.TenHang = Convert.ToString(reader["TenHang"]);
			item.DonViTinh = Convert.ToString(reader["DonViTinh"]);
			item.SoLuong = Convert.ToInt64(reader["SoLuong"]);
			item.DonGia = Convert.ToInt64(reader["DonGia"]);
			item.ThanhTien = Convert.ToInt64(reader["ThanhTien"]);
            item.IsDelete = Convert.ToInt32(reader["IsDelete"]);
			lst.Add(item);
			}
			}
			result.Result = lst;
			result.ReturnCode = 1;
			
			return result;
		}
	}
}
